package com.fast.controller;

import java.security.Principal;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.fast.common.web.AbstractController;
import com.fast.common.web.Response;
import com.fast.modules.acd.entity.HangupParam;
import com.fast.modules.acd.entity.SeatParam;
import com.fast.modules.acd.service.SeatService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import io.swagger.annotations.ApiResponse;

/**
 * 面向坐席端暴露的controller
 * @author yaoyuquan
 *
 */
@RestController
@RequestMapping("/seat")
@Api(tags = "座席状态相关操作controller")
public class SeatController extends AbstractController {
	
	@Autowired
	SeatService seatService;
	
	/**
	 * 获取所有坐席的状态
	 * @return
	 */
	@GetMapping("/status")
	@ApiOperation(value="获取所有坐席状态", notes="这个是测试用的方法，可以查看当前所有在线的坐席以及坐席的状态信息", 
	    tags= {"tag1","tag2"}, response=Response.class, produces="application/json")
	@ApiResponse(code = 200, message = "返回调用结果的包装对象")
	public Response status() {
		return success(seatService.status());
	}

	/**
	 * 上线通知的方法
	 * @param seat
	 * @return
	 */
	@PostMapping("/online")
	@ApiOperation(value="坐席上线", notes="在线客服登录后，需要调用这个接口通知系统修改客服在线状态。传入的参数包括客服ID和客服的技能ID列表", 
        tags= {"tag1","tag2"}, response=Response.class, produces="application/json", consumes="application/json")
	@ApiParam(name="seat", value="坐席上线信息，包括坐席ID和坐席技能列表") 
	public Response online(@RequestBody SeatParam seat) {
		
		seatService.online(seat.getSeatID(), seat.getSkills());
		
		return success();
	}
	
	/**
	 * 坐席下线的方法
	 * @param id
	 * @return
	 */
	@PostMapping("/offline")
	@ApiOperation(value="坐席下线", notes="坐席触发任何不能接听用户请求的状态后，需要调用这个接口，会把坐席信息从存储中删除", 
    tags= {"tag1","tag2"}, response=Response.class, produces="application/json", consumes="application/json")
	@ApiParam(name="id", required=true, value="坐席ID", example="123") 
	public Response offline(@RequestBody String id) {
		
		seatService.offline(id);
		
		return success();
	}
	
	/**
	 * 坐席挂断的方法
	 * @param param
	 * @return
	 */
	@PostMapping("/hangup")
	@ApiOperation(value="会话挂断", notes="会话挂断有可能是用户挂断，也可能是坐席挂断，或者网络中断，挂断后需要调用这个接口修改坐席的会话数据", 
    tags= {"tag1","tag2"}, response=Response.class, produces="application/json", consumes="application/json")
	@ApiParam(name="param", required=true, value="坐席和客户的ID") 
	public String hangup(@RequestBody HangupParam param) {
		
		seatService.hangup(param.getSeatID(), param.getClientID());
		
		return "OK";
	}
	
	
	@GetMapping("/busy")
	@ApiOperation(value="置忙", notes="置忙，置忙后不会再分配新的会话", 
    tags= {"tag1","tag2"}, response=Response.class, produces="application/json", consumes="application/json")
	public Response busy(Principal principal) {
		
		String username = principal.getName();
		seatService.busy(username);
		
		return success();
	}
	
	@GetMapping("/available")
	@ApiOperation(value="取消置忙", notes="取消置忙，取消置忙后可以再接入新的会话", 
		    tags= {"tag1","tag2"}, response=Response.class, produces="application/json", consumes="application/json")
	public Response available(Principal principal) {
		
		String username = principal.getName();
		seatService.available(username);
		
		return success();
	}

}
